package com.smartwebee.android.blespp.hospital.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

import com.smartwebee.android.blespp.R;
import com.smartwebee.android.blespp.databinding.AdapterPlanChildBinding;
import com.smartwebee.android.blespp.databinding.AdapterPlanParentBinding;
import com.smartwebee.android.blespp.hospital.fragment.SelfTestFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by steward on 2018/7/5.
 */

public class SurveyTestExpandableAdapter extends BaseExpandableListAdapter {
    private Question question;
    private Context context;
    private String type;

    public SurveyTestExpandableAdapter(Context context, String type) {
        this.context = context;
        this.type = type;
        initQuestion();
    }

    private void initQuestion() {
        question = new Question();
        if (type.equals(SelfTestFragment.TYPE1)) {
            initType1();
        } else if (type.equals(SelfTestFragment.TYPE2)) {
            initType2();
        } else if (type.equals(SelfTestFragment.TYPE3)) {
            initType3();
        } else if (type.equals(SelfTestFragment.TYPE4)) {
            initType4();
        }
    }

    private void initType4() {
        Item item1 = new Item();
        item1.setTitle("1.跛行");
        item1.setChild(Arrays.asList(
                new Child("无", 5),
                new Child("轻度或间歇跛行", 3),
                new Child("严重或持续跛行", 0)
        ));

        Item item2 = new Item();
        item2.setTitle("2.肿胀");
        item2.setChild(Arrays.asList(
                new Child("无", 10),
                new Child("过度用力后肿胀", 6),
                new Child("平时用力后", 2),
                new Child("持续肿胀", 0)
        ));

        Item item3 = new Item();
        item3.setTitle("3.支持");
        item3.setChild(Arrays.asList(
                new Child("无", 5),
                new Child("手杖或拐杖", 2),
                new Child("不能负重", 0)
        ));

        Item item4 = new Item();
        item4.setTitle("4.上楼");
        item4.setChild(Arrays.asList(
                new Child("无问题", 10),
                new Child("轻度减弱", 6),
                new Child("每一步都困难", 2),
                new Child("不能上楼", 0)
        ));

        Item item5 = new Item();
        item5.setTitle("5.绞索");
        item5.setChild(Arrays.asList(
                new Child("无绞锁或卡感", 15),
                new Child("有绞锁但无卡感", 10),
                new Child("绞锁偶然", 6),
                new Child("绞锁经常", 2),
                new Child("体检时绞锁", 0)
        ));

        Item item6 = new Item();
        item6.setTitle("6.下蹲");
        item6.setChild(Arrays.asList(
                new Child("无问题", 5),
                new Child("轻度减弱", 4),
                new Child("不大于90度", 2),
                new Child("不能下蹲", 0)
        ));

        Item item7 = new Item();
        item7.setTitle("7.不稳定");
        item7.setChild(Arrays.asList(
                new Child("从无打软", 25),
                new Child("运动或费力时偶有打软", 20),
                new Child("运动或费力时常有打软", 15),
                new Child("日常生活偶有", 10),
                new Child("日常生活常发", 5),
                new Child("每一步", 0)
        ));
        question.setQuestions(Arrays.asList(item1, item2, item3, item4, item5, item6, item7));
    }

    private void initType3() {
        Item item1 = new Item();
        item1.setTitle("1.疼痛");
        item1.setChild(Arrays.asList(
                new Child("任何时候均无疼痛", 30),
                new Child("行走时无疼痛", 15),
                new Child("行走时轻度疼痛", 10),
                new Child("行走时中度疼痛", 5),
                new Child("行走时严重疼痛", 0)
        ));

        Item item2 = new Item();
        item2.setTitle("2.疼痛");
        item2.setChild(Arrays.asList(
                new Child("任何时候均无疼痛", 30),
                new Child("休息时无疼痛", 15),
                new Child("休息时轻度疼痛", 10),
                new Child("休息时中度疼痛", 5),
                new Child("休息时严重疼痛", 0)
        ));

        Item item3 = new Item();
        item3.setTitle("3.功能");
        item3.setChild(Arrays.asList(
                new Child("行走站立无限制", 22),
                new Child("行走2500～5000米和站立半小时以上", 10),
                new Child("行走500～2500米和站立可达半小时", 8),
                new Child("行走少于500米", 4),
                new Child("不能行走", 0)
        ));

        Item item4 = new Item();
        item4.setTitle("4.功能");
        item4.setChild(Arrays.asList(
                new Child("行走站立无限制", 22),
                new Child("屋内行走，无需支具", 5),
                new Child("屋内行走，需要支具", 2),
                new Child("能上楼梯", 5),
                new Child("能上楼梯，但需支具", 2)
        ));

        Item item5 = new Item();
        item5.setTitle("5.肌力");
        item5.setChild(Arrays.asList(
                new Child("优：完全能对抗阻力", 10),
                new Child("良：部分对抗阻力", 8),
                new Child("中：能带动关节活动", 4),
                new Child("差：不能带动关节活动", 0)
        ));

        Item item6 = new Item();
        item6.setTitle("6.屈曲畸形");
        item6.setChild(Arrays.asList(
                new Child("无畸形", 10),
                new Child("小于5度", 8),
                new Child("5~10度", 5),
                new Child("大于10度", 0)
        ));

        Item item7 = new Item();
        item7.setTitle("7.稳定性");
        item7.setChild(Arrays.asList(
                new Child("正常", 10),
                new Child("轻度不稳0～5度", 8),
                new Child("中度不稳5～15度", 5),
                new Child("严重不稳大于15度", 0)
        ));
        question.setQuestions(Arrays.asList(item1, item2, item3, item4, item5, item6, item7));
    }

    private void initType2() {
        Item item1 = new Item();
        item1.setTitle("1.在平坦的地面上行走");
        item1.setChild(Arrays.asList(
                new Child("0", 0),
                new Child("1", 1),
                new Child("2", 2),
                new Child("3", 3),
                new Child("4", 4),
                new Child("5", 5),
                new Child("6", 6),
                new Child("7", 7),
                new Child("8", 8),
                new Child("9", 9),
                new Child("10", 10)

        ));

        Item item2 = new Item();
        item2.setTitle("2.上楼梯或下楼梯");
        item2.setChild(Arrays.asList(
                new Child("0", 0),
                new Child("1", 1),
                new Child("2", 2),
                new Child("3", 3),
                new Child("4", 4),
                new Child("5", 5),
                new Child("6", 6),
                new Child("7", 7),
                new Child("8", 8),
                new Child("9", 9),
                new Child("10", 10)
        ));

        Item item3 = new Item();
        item3.setTitle("3.晚上，尤其影响睡眠的疼痛");
        item3.setChild(Arrays.asList(
                new Child("0", 0),
                new Child("1", 1),
                new Child("2", 2),
                new Child("3", 3),
                new Child("4", 4),
                new Child("5", 5),
                new Child("6", 6),
                new Child("7", 7),
                new Child("8", 8),
                new Child("9", 9),
                new Child("10", 10)
        ));

        Item item4 = new Item();
        item4.setTitle("4.坐着或躺着");
        item4.setChild(Arrays.asList(
                new Child("0", 0),
                new Child("1", 1),
                new Child("2", 2),
                new Child("3", 3),
                new Child("4", 4),
                new Child("5", 5),
                new Child("6", 6),
                new Child("7", 7),
                new Child("8", 8),
                new Child("9", 9),
                new Child("10", 10)
        ));

        Item item5 = new Item();
        item5.setTitle("5.挺直身体站立");
        item5.setChild(Arrays.asList(
                new Child("0", 0),
                new Child("1", 1),
                new Child("2", 2),
                new Child("3", 3),
                new Child("4", 4),
                new Child("5", 5),
                new Child("6", 6),
                new Child("7", 7),
                new Child("8", 8),
                new Child("9", 9),
                new Child("10", 10)
        ));

        Item item6 = new Item();
        item6.setTitle("6.早晨起床时僵硬情况有多严重");
        item6.setChild(Arrays.asList(
                new Child("0", 0),
                new Child("1", 1),
                new Child("2", 2),
                new Child("3", 3),
                new Child("4", 4),
                new Child("5", 5),
                new Child("6", 6),
                new Child("7", 7),
                new Child("8", 8),
                new Child("9", 9),
                new Child("10", 10)
        ));

        Item item7 = new Item();
        item7.setTitle("7.僵硬状况在以后的时间内，坐、卧或休息之后有多严重");
        item7.setChild(Arrays.asList(
                new Child("0", 0),
                new Child("1", 1),
                new Child("2", 2),
                new Child("3", 3),
                new Child("4", 4),
                new Child("5", 5),
                new Child("6", 6),
                new Child("7", 7),
                new Child("8", 8),
                new Child("9", 9),
                new Child("10", 10)
        ));
        question.setQuestions(Arrays.asList(item1, item2, item3, item4, item5, item6, item7));
    }

    private void initType1() {
        Item item1 = new Item();
        item1.setTitle("1.是否跛行");
        item1.setChild(Arrays.asList(
                new Child("无", 5),
                new Child("轻度或周期性", 3),
                new Child("持续严重", 0)
        ));

        Item item2 = new Item();
        item2.setTitle("2.负重行走时疼痛情况");
        item2.setChild(Arrays.asList(
                new Child("完全负重没有疼痛", 5),
                new Child("有疼痛", 3),
                new Child("严重疼痛无法忍受", 0)
        ));

        Item item3 = new Item();
        item3.setTitle("3.行走");
        item3.setChild(Arrays.asList(
                new Child("无限制", 5),
                new Child("超过200m", 3),
                new Child("可行走1-2km", 2),
                new Child("无法行走", 0)
        ));

        Item item4 = new Item();
        item4.setTitle("4.爬楼梯");
        item4.setChild(Arrays.asList(
                new Child("无困难", 10),
                new Child("上楼梯轻微疼痛", 8),
                new Child("上下楼梯时都疼痛", 5),
                new Child("不能爬楼梯", 0)
        ));

        Item item5 = new Item();
        item5.setTitle("5.下蹲");
        item5.setChild(Arrays.asList(
                new Child("无困难", 5),
                new Child("下蹲多时疼痛", 4),
                new Child("每次下蹲都出现疼痛", 3),
                new Child("下蹲过度时疼痛", 2),
                new Child("不能下蹲", 0)
        ));

        Item item6 = new Item();
        item6.setTitle("6.跑步或者慢跑");
        item6.setChild(Arrays.asList(
                new Child("无困难", 10),
                new Child("超过2km出现疼痛", 8),
                new Child("开始跑时轻微疼痛", 6),
                new Child("严重疼痛", 3),
                new Child("不能", 0)
        ));

        Item item7 = new Item();
        item7.setTitle("7.跳跃");
        item7.setChild(Arrays.asList(
                new Child("无困难", 10),
                new Child("有轻微困难", 7),
                new Child("持续疼痛", 2),
                new Child("不能", 0)
        ));
        question.setQuestions(Arrays.asList(item1, item2, item3, item4, item5, item6, item7));
    }

    @Override
    public int getGroupCount() {
        return question.getQuestions().size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return question.getQuestions().get(groupPosition).getChild().size();
    }

    @Override
    public Item getGroup(int groupPosition) {
        return question.getQuestions().get(groupPosition);
    }

    @Override
    public Child getChild(int groupPosition, int childPosition) {
        return question.getQuestions().get(groupPosition).getChild().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        AdapterPlanParentBinding binding;
        if (convertView == null) {
            binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.adapter_plan_parent, parent, false);
            convertView = binding.getRoot();
            convertView.setTag(binding);
        } else {
            binding = (AdapterPlanParentBinding)convertView.getTag();
        }
        binding.tvName.setText(question.getQuestions().get(groupPosition).getTitle());
        return convertView;
    }



    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        AdapterPlanChildBinding binding;
        if (convertView == null) {
            binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.adapter_plan_child, parent, false);
            convertView = binding.getRoot();
            convertView.setTag(binding);
        } else {
            binding = (AdapterPlanChildBinding)convertView.getTag();
        }
        binding.tvName.setText(getChild(groupPosition, childPosition).getDesc());
        binding.tvName.setTextColor(getChild(groupPosition, childPosition).isSelected() ? context.getResources().getColor(R.color.red) : context.getResources().getColor(R.color.light_blue));
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public class Question {
        private List<Item> questions;

        public List<Item> getQuestions() {
            return questions;
        }

        public void setQuestions(List<Item> questions) {
            this.questions = questions;
        }
    }
    public class Item {
        private String title;
        private List<Child> child;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public List<Child> getChild() {
            return child;
        }

        public void setChild(List<Child> child) {
            this.child = child;
        }
    }
    public class Child {
        private String desc;
        private int score;
        private boolean isSelected = false;

        public boolean isSelected() {
            return isSelected;
        }

        public void setSelected(boolean selected) {
            isSelected = selected;
        }

        public Child(String desc, int score) {
            this.desc = desc;
            this.score = score;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }

        public int getScore() {
            return score;
        }

        public void setScore(int score) {
            this.score = score;
        }
    }
}
